package com.jkgh.tnh.rs;

import com.jkgh.asin.*;
import com.jkgh.asin.buffers.*;
import com.jkgh.remedium.*;
import com.jkgh.remedium.objects.*;
import com.jkgh.remedium.objects.messages.*;
import com.jkgh.remedium.states.*;
import com.jkgh.remedium.tools.*;

/**
 * The access class of this system.
 * 
 * @author RS2JGenerator
 *
 */
public class TNHAccess extends RemediumDataObject {

	/**
	 * Version of this TNH Server.
	 */
	public int version;

	/**
	 * Alternative access object.
	 */
	public TNHAccess alternative;

	/**
	 * Last few messages of the day.
	 */
	public String[] motds;

	/**
	 * Default constructor.
	 */
	public TNHAccess() {
	}

	/**
	 * Setting constructor.
	 * 
	 * @param version Version of this TNH Server.
	 * @param alternative Alternative access object.
	 * @param motds Last few messages of the day.
	 */
	public TNHAccess(int version, TNHAccess alternative, String[] motds) {
		this.version = version;
		this.alternative = alternative;
		this.motds = motds;
	}

	// --------------------------------------------------------------------------------

	@Override
	public void produceContent(DataConsumer consumer, RemediumOutMessage message) {
		consumer.consumeInt(version);;
		message.produceObject(alternative, consumer);
		RemediumTools.produceStringArray(consumer, motds);
	}

	public static void consume(StateMachine machine, final RemediumInMessage message, final Future<TNHAccess> future) {
		machine.setCurrentState(new GatheringState(machine, 1) {

			@Override
			protected void onNeededBytesAvailable() {
				switch (machine.produceByte()) {

				case RemediumConstants.NULL:
					future.goWith(null);
					break;

				case RemediumConstants.HISTORY:
					future.goWith((TNHAccess) message.fromHistory(machine.produceInt()));
					break;

				case RemediumConstants.VALUE:
					consumeContent(machine, message, future);
					break;
				}
			}
		});
	}

	public static void consumeArray(final StateMachine machine, final RemediumInMessage message, final Future<TNHAccess[]> future) {
		machine.setCurrentState(new GatheringState(machine, 4) {

			@Override
			protected void onNeededBytesAvailable() {
				final int length = machine.produceInt();
				if (length == -1) {
					future.goWith(null);
				} else if (length == 0) {
					future.goWith(new TNHAccess[0]);
				} else {
					consume(machine, message, new Future<TNHAccess>() {

						private int i = 0;
						private TNHAccess[] result = new TNHAccess[length];

						@Override
						public void goWith(TNHAccess o) {
							result[i++] = o;
							if (i == length) {
								future.goWith(result);
							} else {
								consume(machine, message, this);
							}
						}
					});
				}
			}
		});
	}

	private static void consumeContent(final StateMachine machine, final RemediumInMessage message, final Future<TNHAccess> future) {
		final TNHAccess ret = new TNHAccess();
		message.toHistory(ret);
		RemediumTools.consumeInt(machine, new Future<Integer>() {

			@Override
			public void goWith(Integer o) {
				ret.version = o;

				TNHAccess.consume(machine, message, new Future<TNHAccess>() {

					@Override
					public void goWith(TNHAccess o) {
						ret.alternative = o;

						RemediumTools.consumeStringArray(machine, new Future<String[]>() {

							@Override
							public void goWith(String[] o) {
								ret.motds = o;

								future.goWith(ret);
							}
						});
					}
				});
			}
		});
	}
}
